
import re

# openfile = alteon configuration file path as the source
#transFile =  LTM configuration  file path as the destination
openFile = open( '/Users/k.ji/vscode/PycharmProjects/AlteonPY/chinamoney/altconfig-master.txt' )
transFile = open('/Users/k.ji/vscode/PycharmProjects/AlteonPY/chinamoney/altconfig-master-l4.txt','a')

initConfig = openFile.read()
readConfig = initConfig.split('\n')
configLenth = len(readConfig)





memberList = [] # memberList Structure :  [[memberName,memberIP,memberPort] ]
groupList = []  # groupList Structure : [  [groupName,[memberName1,memberName2,memberName3..]] ]
vsList = [] # vsList Structure : [vsName,vip,vPort,protocol,rport,groupName]

for i in range(configLenth):
	if '/c/slb/real ' in readConfig[i]:
		memberName = readConfig[i].split(" ")[1]
		memberPort = 'noAddport'
		temp = i
		while True:
			temp = temp + 1
			if '/c/' in readConfig[temp]:
				break
			if '	rip' in readConfig[temp]:
				memberIP = readConfig[temp].split(" ")[1]
			if '	addport' in readConfig[temp]:
				memberPort = readConfig[temp].split(" ")[1]
		memberList.append([memberName,memberIP,memberPort])
	if '/c/slb/group ' in readConfig[i]:
		groupName = readConfig[i].split(" ")[1]
		groupList.append([groupName,[]])
		temp = i
		while True:
			temp = temp + 1
			if '/c/' in readConfig[temp]:
				break
			if '	add ' in readConfig[temp]:
				memberName = readConfig[temp].split(" ")[1]
				groupList[ len(groupList) - 1][1].append(memberName)
	
	
for i in range(configLenth):	
	if '/c/slb/virt ' in readConfig[i] and '/service ' not in readConfig[i]:
		vsName = readConfig[i].split(" ")[1]
		vip = readConfig[i+3].split(" ")[1]
		temp = i + 3
		done_a = 0
		done_b = 0
		done_c = 0
		while True:
			temp = temp + 1
			if '/c/slb/virt ' in readConfig[temp] and vsName != readConfig[temp].split(" ")[1].split("/")[0]:
				break
			if "/c/" in readConfig[temp] and '/c/slb/virt ' not in readConfig[temp]:
				break
			if '/c/slb/virt ' + vsName + '/service 'in readConfig[temp]:
				vPort = readConfig[temp].split(" ")[2]
				protocol = readConfig[temp].split(" ")[3]
				done_a = 1
			if '	group ' in readConfig[temp]:
				groupName = readConfig[temp].split(" ")[1]
				done_b = 1
			if '	rport ' in readConfig[temp]:
				rport = readConfig[temp].split(" ")[1]
				done_c = 1
			if done_a == 1 and done_b == 1 and done_c ==1:
				vsList.append([vsName,vip,vPort,protocol,rport,groupName])
				done_a = 0
				done_b = 0
				done_c = 0



for vs in vsList:
	vsName = vs[0]
	vip = vs[1]
	vPort = vs[2]
	protocol = vs[3]
	rport = vs[4]
	groupName = vs[5]
	if rport == '0':
		for group in groupList:
			if groupName == group[0]:
				groupConfig = 'tmsh create ltm pool Pool_' + groupName + ' members add { '
				transFile.write(groupConfig)
				for memberName in group[1]:
					for member in memberList:
						if memberName == member[0]:
							memberIP = member[1]
							memberPort = member[2]
							groupConfig = memberIP + ':' + memberPort + ' { address ' + memberIP + ' } '
							transFile.write(groupConfig)
				groupConfig = '} monitor tcp load-balancing-mode round-robin \n'
				transFile.write(groupConfig)
	if rport != '0':
		for group in groupList:
			if groupName == group[0]:
				groupConfig = 'tmsh create ltm pool Pool_' + groupName + ' members add { '
				transFile.write(groupConfig)
				for memberName in group[1]:
					for member in memberList:
						if memberName == member[0]:
							memberIP = member[1]
							memberPort = rport
							groupConfig = memberIP + ':' + memberPort + ' { address ' + memberIP + ' } '
							transFile.write(groupConfig)
				groupConfig = '} monitor tcp load-balancing-mode round-robin \n'
				transFile.write(groupConfig)
	if protocol == 'http' or protocol == 'https':
			vsConfig = 'tmsh create ltm virtual %s destination %s:%s pool Pool_%s profiles add { HTTP } source-address-translation { type automap } persist replace-all-with { source_addr }' %(vsName,vip,vPort,groupName)		
	else:
		vsConfig = 'tmsh create ltm virtual %s destination %s:%s pool Pool_%s ip-protocol tcp source-address-translation { type automap } persist replace-all-with { source_addr }' %(vsName,vip,vPort,groupName)
	transFile.write(vsConfig  + '\n')